package com.woniuxy.rms.service.impl;

import com.woniuxy.rms.dao.MenusMapper;
import com.woniuxy.rms.dao.PermissionMapper;
import com.woniuxy.rms.pojo.Admins;
import com.woniuxy.rms.pojo.Permission;
import com.woniuxy.rms.pojo.RoleMenu;
import com.woniuxy.rms.service.PermissionService;
import com.woniuxy.rms.util.DataResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Auther: Chengang  <br/>
 * Date: 2020/3/25 0025:上午 11:48  <br/>
 * Description:权限服务层
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private MenusMapper menusMapper;
    @Override
    public List<Permission> findAll() {
        return permissionMapper.findAll();
    }

    @Override
    public List<Permission> findByPage(Integer currentPage, Integer pageSize) {
        return permissionMapper.findByPage(currentPage,pageSize);
    }

    @Override
    public DataResult savePerm(Permission perm) {
        //判断权限是否重复
        List<Permission> currentPerms = permissionMapper.findByPerm(perm.getPermission());
        for (Permission currentPerm : currentPerms) {
            if (!currentPerm.getId().equals(perm.getId())){
                return DataResult.failed("权限重复！");
            }
        }
        //获取当前时间
        perm.setCreateTime(new Date());
        //获取登录信息
        Admins admins = (Admins) SecurityUtils.getSubject().getPrincipal();
        perm.setAdminId(admins.getId());
        //存储数据
        permissionMapper.insertSelective(perm);
        return DataResult.success();
    }

    @Override
    public DataResult batchDel(String ids) {
        //将id串转数组
        List<Integer> permIds = Arrays.stream(ids.split(",")).map(s -> Integer.parseInt(s)).collect(Collectors.toList());
        //判断权限是否被分配
        List<RoleMenu> roleMenus = menusMapper.findRoleMenuByPid(permIds);
        if (roleMenus.size() > 0){
            return DataResult.failed("权限已经被分配，不能删除！");
        }
        //删除菜单权限
        menusMapper.batchDelMenuBypermIds(permIds);
        //删除权限
        permissionMapper.batchDelByIds(permIds);
        return DataResult.success();
    }

    @Override
    public DataResult delById(Integer id) {
        List<Integer> permIds = new ArrayList<>();
        permIds.add(id);
        //判断权限是否被分配
        List<RoleMenu> roleMenus = menusMapper.findRoleMenuByPid(permIds);
        if (roleMenus.size() > 0){
            return DataResult.failed("权限已经被分配，不能删除！");
        }
        //删除菜单权限
        menusMapper.batchDelMenuBypermIds(permIds);
        //删除权限
        permissionMapper.deleteByPrimaryKey(id);
        return DataResult.success();
    }

    @Override
    public Permission findById(Integer id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public DataResult editPerm(Permission perm) {
        //判断权限是否重复
        List<Permission> currentPerms = permissionMapper.findByPerm(perm.getPermission());
        for (Permission currentPerm : currentPerms) {
            if (!currentPerm.getId().equals(perm.getId())){
                return DataResult.failed("权限重复！");
            }
        }
        //传入修改时间
        perm.setUpdateTime(new Date());
        permissionMapper.updateByPrimaryKeySelective(perm);
        return DataResult.success();
    }
}